package com.daguchuangyi.erp_android.production_detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.product.Shelf;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShelfFragment extends Fragment {
    private static final String TAG_SHELF_LIST = "TAG_SHELF_LIST";

    private RecyclerView mSampleRv;
    private ProductSampleAdapter mSampleAdapter;

    private List<Shelf> mShelves = new ArrayList<>();

    public ShelfFragment() {
        // Required empty public constructor
    }

    public static ShelfFragment getInstance(ArrayList<Shelf> shelves) {
        ShelfFragment shelfFragment = new ShelfFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(TAG_SHELF_LIST, shelves);
        shelfFragment.setArguments(bundle);
        return shelfFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSampleRv = (RecyclerView) view.findViewById(R.id.sample_rv);
        mSampleRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mSampleAdapter = new ProductSampleAdapter(R.layout.item_product_detail_sample_layout, null);
//        mSampleRv.setNestedScrollingEnabled(true);
        mSampleRv.setAdapter(mSampleAdapter);

        if (getArguments() != null) {
            ArrayList<Shelf> shelves = getArguments().getParcelableArrayList(TAG_SHELF_LIST);
            mSampleAdapter.setNewData(shelves);

            View headerView = LayoutInflater.from(getContext()).inflate(R.layout.header_sample_produt_layout,
                    (ViewGroup) mSampleRv.getParent(), false);
            mSampleAdapter.addHeaderView(headerView);
        }
    }

    private class ProductSampleAdapter extends BaseQuickAdapter<Shelf, BaseViewHolder> {

        public ProductSampleAdapter(int layoutResId, @Nullable List<Shelf> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Shelf item) {
            helper.setText(R.id.position_info, item.getSNO())
                    .setText(R.id.amount_info, String.valueOf(item.getNumber()))
                    .setText(R.id.color_info, item.getColor())
                    .setText(R.id.remark_info, item.getRemark());
        }
    }

}
